<template>
  <div class="list-table">
    <el-form ref="queryForm" :model="queryParams" :inline="true" @submit.prevent.native>
      <el-form-item :label="$t('证券公司名称')" prop="securityName">
        <el-input
          v-model="queryParams.securityName"
          :placeholder="$t('证券公司名称')"
          clearable
          :onsubmit="false"
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="listedSecurityList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('证券公司名称')" align="center" prop="securityName" min-width="150" />
      <el-table-column :label="$t('证券账号')" align="center" prop="securityAccount" min-width="140" />
      <el-table-column :label="$t('第三方存管账户')" align="center" prop="thirdAccount" min-width="130" />
      <el-table-column :label="$t('存管账户开户行')" align="center" prop="bankAccount" min-width="130" />
      <el-table-column :label="$t('对应投资主体')" align="center" prop="investorName" min-width="150" />
      <el-table-column :label="$t('是否是创投基金')" align="center" prop="ventureFund" :formatter="ventureFundFormat" min-width="120" />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="140">
        <template slot-scope="scope">
          <el-button
            v-if="!scope.row.selectFlag"
            v-hasPermi="['listed:security:view']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="selectHandle(scope.row)"
          >{{ $t('选择') }}</el-button>
          <el-button
            v-else
            v-hasPermi="['listed:security:view']"
            size="small"
            disabled
            type="text"
            icon="el-icon-edit"
            @click="selectHandle(scope.row)"
          >{{ $t('已选择') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listListedSecurity } from '@/api/deal/listed/security/listedSecurity'
export default {
    components: {
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 上市项目-证券账户管理表格数据
          listedSecurityList: [],
          // 是否是创投基金字典
          ventureFundOptions: [],
          // 持仓信息集合
          positionHoldList: [],
          // 项目解禁列表
          releaseList: [],
          searchType: '',
          // 查询参数
          queryParams: {
              listedId: undefined,
              securityName: undefined,
              securityAccount: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {}
      }
  },
  created() {
      this.getDicts('sys_yes_no').then(response => {
          this.ventureFundOptions = response.data
      })
  },
  methods: {
    /** 查询上市项目-证券账户管理列表 */
    getList() {
        this.loading = true
        this.queryParams.dealType = (this.$route.query.dealType === undefined ? 'equity' : this.$route.query.dealType)
        listListedSecurity(this.queryParams).then(response => {
            this.listedSecurityList = response.rows
            this.total = response.total
            if (this.searchType === 'listedPositionHoldForm') {
              this.judgeSelect(this.positionHoldList)
            } else if (this.searchType === 'listedReleaseForm') {
              this.judgeSelect(this.releaseList)
            }
            this.loading = false
        })
    },
    // 是否是创投基金字典翻译
    ventureFundFormat(row) {
      return this.selectDictLabel(this.ventureFundOptions, row.ventureFund)
    },
    /** 选择按钮操作 */
    selectHandle(row) {
      if (this.searchType === 'listedPositionHoldForm') {
        this.$parent.$parent.listOpen = false
        this.$parent.$parent.listedPositionHoldHandle(row)
      } else if (this.searchType === 'listedReleaseForm') {
        this.$parent.$parent.listOpen = false
        this.$parent.$parent.listedReleaseHandle(row)
      }
    },
    /** 选择按钮操作  请求来源：listedPositionHoldForm listedReleaseForm*/
    setType(type, list) {
      this.searchType = type
      this.positionHoldList = []
      this.releaseList = []
      if (this.searchType === 'listedPositionHoldForm') {
        this.positionHoldList = list
        this.judgeSelect(this.positionHoldList)
      } else if (this.searchType === 'listedReleaseForm') {
        this.releaseList = list
        this.judgeSelect(this.releaseList)
      }
    },
    // 判断当前集合列表中的数据是否已经选过
    judgeSelect(list) {
      this.loading = true
      this.queryParams.dealType = (this.$route.query.dealType === undefined ? 'equity' : this.$route.query.dealType)
      listListedSecurity(this.queryParams).then(response => {
        this.listedSecurityList = response.rows
        list.forEach(position => {
          this.listedSecurityList.forEach(item => {
            if (item.selectFlag !== true) {
              if (item.id === position.securityId) {
                item.selectFlag = true
              } else {
                item.selectFlag = false
              }
            }
          })
        })
        this.total = response.total
        this.loading = false
      })
    },
    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            listedId: undefined,
            securityName: undefined,
            securityAccount: undefined,
            thirdAccount: undefined,
            bankAccount: undefined,
            investorId: undefined,
            ventureFund: '0',
            selectFlag: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined,
            remark: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        if (this.searchType === 'listedPositionHoldForm') {
          this.judgeSelect(this.positionHoldList)
        } else if (this.searchType === 'listedReleaseForm') {
          this.judgeSelect(this.releaseList)
        }
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    }
  }
}
</script>
